
const TIME_ONE = 60000;
const TIME_TWO = 1000;
const TIME_THREE = 10;

export function timeConvert(time: number): string {
  let min: number = Math.floor(time / TIME_ONE);
  let second: string = ((time % TIME_ONE) / TIME_TWO).toFixed(0);
  return `${min}:${(+second < TIME_THREE ? '0' : '') + second}`;
}

export function secondToTime(seconds: number): string {
  const SECONDS_PER_HOUR = 3600; // 1小时 = 3600秒
  const SECONDS_PER_MINUTE = 60; // 1分钟 = 60秒

  // 计算小时、分钟和秒
  const hour = Math.floor(seconds / SECONDS_PER_HOUR);
  const minute = Math.floor((seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
  const second = seconds % SECONDS_PER_MINUTE;

  // 格式化时间，补零
  const hourStr = hour < 10 ? `0${hour}` : `${hour}`;
  const minuteStr = minute < 10 ? `0${minute}` : `${minute}`;
  const secondStr = second < 10 ? `0${second}` : `${second}`;

  // 根据时间长度返回格式化字符串
  if (hour > 0) {
    return `${hourStr}:${minuteStr}:${secondStr}`; // 时:分:秒
  }
  if (minute > 0) {
    return `${minuteStr}:${secondStr}`; // 分:秒
  }
  return `00:${secondStr}`; // 00:秒
}

export function convertTimestampToDate(timestamp: number): string {
  const date = new Date(timestamp);

  // 时间元素提取与补零处理
  const year = date.getFullYear();
  const month = (date.getMonth()  + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2,  '0');
  const hours = date.getHours().toString().padStart(2,  '0');
  const minutes = date.getMinutes().toString().padStart(2,  '0');
  const seconds = date.getSeconds().toString().padStart(2,  '0');

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 示例调用
// const timestamp = 1678639620; // 示例时间戳
// console.log(convertTimestampToDate(timestamp));

